package com.miraclemiles.oauth.config;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.oauth2.provider.code.RandomValueAuthorizationCodeServices;

import com.miraclemiles.oauth.service.RedisAuthorizationCodeServices;
import com.miraclemiles.oauth.service.RedisClientDetailsService;

@Configuration
public class RedisClientDetailsConfig {

    @Autowired
    private DataSource dataSource;

    @Autowired
    private RedisConnectionFactory redisConnectionFactory;

    /**
     * 声明 ClientDetails实现
     */
    @Bean
    public RedisClientDetailsService redisClientDetailsService() {
        return new RedisClientDetailsService(dataSource);
    }

    @Bean
    public RandomValueAuthorizationCodeServices authorizationCodeServices() {
        return new RedisAuthorizationCodeServices(redisTemplate());
    }

    @Bean
    public RedisTemplate<String, Object> redisTemplate() {
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>();
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        return redisTemplate;
    }
}
